package com.lagou.controller;

import com.google.common.cache.LoadingCache;
import com.lagou.bean.HotPositionDto;
import com.lagou.bean.Result;
import com.lagou.pojo.HotPosition;
import com.lagou.service.GuavaCacheService;
import com.lagou.service.HotPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author senyang
 */
@Controller
public class HomeController {

    @Autowired
    HotPositionService hotPositionService;

    @Value("${default.pageNo}")
    Integer defaultPageNo;

    @Value("${default.pageSize}")
    Integer defaultPageSize;


    @Autowired
    GuavaCacheService cacheService;

    @RequestMapping("/home")
    public String toLoginPage(Model model){
        Result test = (Result)cacheService.getHotPosGuavaCache();
        model.addAttribute("data", test.getResult());
        return "client/index";
    }


    @RequestMapping("/getHot")
    @ResponseBody
    public Result find(){
        Page<HotPosition> all = hotPositionService.findAll(defaultPageNo,defaultPageSize);
        return Result.ok(all);
    }


    @PostMapping("/add/pos")
    @ResponseBody
    public Result addPos(@RequestBody HotPositionDto dto) {
        return hotPositionService.addHotPos(dto);
    }

    @DeleteMapping("/del/pos")
    @ResponseBody
    public Result delHotPos(@RequestParam("id") Integer id) {
        return hotPositionService.delHotPos(id);
    }
}
